var isDebug = false;
var path = "getQtComponent.jsp?path="
var dotcomPath = "http://quote.morningstar.com/";
var getTickerInfoURL = "http://qt.morningstar.com/switchservice/getids.ashx?";
var qtQueryUtil = {
        args: null,
        queryString: function(key) {
            if (this.args == null) {
                this.args = {};
                var query = location.search.substring(1);
                var pairs = query.split("&");
                for (var i = 0; i < pairs.length; i++) {
                    var pos = pairs[i].indexOf('=');
                    if (pos == -1) {
                        continue;
                    }    
                    var argname = pairs[i].substring(0, pos);
                    var value = pairs[i].substring(pos + 1);
                    this.args[argname] = unescape(value);
                }
            }
            return this.args[key];
        },
		getLinkParametersByKey: function(link, key){
			var temp = link.split("?"), query = temp[temp.length - 1];
			var pairs = query.split("&");
			for (var i = 0; i < pairs.length; i++) {
				var pos = pairs[i].indexOf('=');
				if (pos == -1) {
					continue;
				}
				var argname = pairs[i].substring(0, pos);
				var value = pairs[i].substring(pos + 1);
				this.args[argname] = unescape(value);
			}
		}
    };
function checkTickerInfo(symbol, callback) {
    if (symbol == "") return false;
    var url = getTickerInfoURL + "symbol=" + symbol + "&amp;region=" + region + "&cb=" + callback;
    $.getScript(url);
}
function GetPara() {
	var paras = "t=" + exchTicker;
	if (culture != "")
		paras += "&culture=" + culture;
	if (region != "")
		paras += "&region=" + region;
	paras += "&r=" + Math.floor(Math.random() * 1000000);
	return paras;
}

function LoadAComponent(page, params, bCache, timeout, showItems, hideItems, idContent, callIfFail,failCallback) {
    var t = dotcomPath + page + "?" + params;
    t = encodeURIComponent(t);
    var url = path + t;
	$.ajax({
		cache: bCache,
		timeout: timeout,
		url: url,
		//data: params,
		success: function(data, status) {
            //checkData=data.replace(/\s+<\/?.+?>[\r\n]/g,'');
			if (status != "success" || data == "" || data.indexOf("<title> Morningstar.com Error Page </title>") > 0) {
				if (callIfFail != null) {  // error page 
					for (var i = 0; i < callIfFail.length; i++)
						LoadAComponent(callIfFail[i], params, bCache, timeout, showItems, hideItems, null, null);
				}else{  // customize callback
					if(typeof(failCallback)== "function"){
						failCallback(data);
					}
				}
				return;
			}
			if (showItems != null) {
				for (var i = 0; i < showItems.length; i++)
					$("#" + showItems[i]).show();
			}
			if (hideItems != null) {
				for (var i = 0; i < hideItems.length; i++)
					$("#" + hideItems[i]).hide();
			}
            if(data !=null&&idContent=="analysiscontent"){
                data = data.replace(/\r\n/g,"");
                if(data.length<500){
                  // $("#" + idContent) 
                  //$(".qt-wrap").hide();
                }else{
                    dotcomPath = QS_PROTOCOL+"analysis.morningstar.com/";
                    LoadAComponent("AnalystReport/Handler/StockMSTake.ashx", params, false, 20000, null, null, "msTakeCon");
                    dotcomPath = QS_PROTOCOL+"quote.morningstar.com/";
                    LoadAComponent("stock/Competitors.ashx", params, true, 30000, null, null, "competitorsCon");
                    $(".qt-wrap").show();
                }
            }
            
			if (idContent != null){
                   $("#" + idContent).html(data);
            }   
			if (idContent == "idInsiderActContent" || idContent == "idECompContent" || idContent=="insiderKE" || idContent == "insiderBD") {  // replace hr
			    $("#"+idContent+"").find("a").each(function(){
			             var href = $(this).attr("href");
			              href = href.replace(/Executive-Profile.aspx/g,"eProfile.htm");
			              if(typeof(parentId) != "undefined") href += "&pt="+parentId;
			              $(this).attr("href",href);})
			}
			if (idContent == "eProfile") {
			    $("#"+idContent+"").find(".r_bcrumbs").find("a").each(function(){
			              var href = $(this).attr("href");
			              if(typeof(parentId) != "undefined") href += "&p="+parentId;
			              $(this).attr("href",href);})
			}
			if(typeof(overrideLinks) != "undefined") {
				overrideLinks(idContent);
			}
		},
		error:function(data){
			if(typeof(failCallback)== "function"){
				failCallback(data);
			}
		}
			
	});
}